<template>
  <div>
    <div class="search-content">
      <Col class="panel content-header" :span="24">
      <div class="title-bar cf">
        <h5 class="fl">
          <span>
            <a href="javascript:void(0);" class="link" @click="backToLast">
              <i class="fa fa-angle-left" aria-hidden="true"></i>返回上一页</a>
            <span class="dashboard-card-sep">|</span>
            <span class="dashboard-card-name"><span>月负荷详情</span></span>
          </span>
        </h5>
      </div>
      </Col>
    </div>
    <div>
      <Row>
        <Col span="24" v-if="showTimeRange" style="padding:10px;box-sizing:border-box;">
        <DatePicker :clearable="false" :options="options" v-model="timeDate" type="month" placeholder="请选择查询时间..." style="width: 220px" @on-change="onTimeChange"></DatePicker>
        </Col>
        <!-- 折线图 -->
        <Col span="24" class="panel map-panel" style="margin-bottom:15px;">
        <div style="width:100%;height:350px;" class="blank-chart">
          <div ref="chart" style="width: 100%;height: 100%;"></div>
        </div>
        </Col>
        <!-- 表格 -->
        <Col class="panel table-panel" :span="24" style="overflow-x: auto;margin-bottom: 20px;">
        <AnalTable :tableData="tableData" :tableStyle="{ width:'100%' }"></AnalTable>
        </Col>
      </Row>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex"
import AnalTable from "../AnalTable"
import { getFuhe } from "@/api"
import mixin from "./mixin"

export default {
  mixins: [mixin],
  computed: { ...mapGetters(["color"]) },
  mounted() {
    this.initState()
  },
  components: { AnalTable },
  data: () => ({
    deviceName: "",
    tableData: [],
    showTimeRange: true,
    echartInstence: null
  }),
  methods: {
    onTimeChange() {
      this.initState()
    },
    initState() {
      const { id, name } = this.$route.params
      const vm = this,
        timeDate = vm.timeDate
      vm.deviceName = name
      const date = vm.moment(timeDate).format("YYYY-MM"),
        params = {
          deviceNo: id,
          date
        }
      if (vm.echartInstence) {
        vm.echartInstence.clear()
      }
      vm.$nextTick(() => {
        const dom = vm.$refs.chart
        vm.echartInstence = vm.echarts.init(dom)
        vm.echartInstence.showLoading({ text: "", color: "red" })
      })
      getFuhe(params).then(res => {
        if (res.code === 200 && res.data) {
          vm.printChart(res.data.list, name, date)
          try {
            vm.tableData = [
              {
                key: "最大负荷(kW)",
                value: res.data.maxFuhe
              },
              {
                key: "发生时间",
                value: vm
                  .moment(res.data.maxFuheDate)
                  .format("YYYY-MM-DD HH:mm:ss")
              },
              {
                key: "最小负荷(kW)",
                value: res.data.minFuhe
              },
              {
                key: "发生时间",
                value: vm
                  .moment(res.data.minFuheDate)
                  .format("YYYY-MM-DD HH:mm:ss")
              },
              {
                key: "平均负荷(kW)",
                value: res.data.avgFuhe
              },
              {
                key: "峰谷差(kW)",
                value: res.data.diffFuhe
              },
              {
                key: "峰谷差率(%)",
                value: res.data.diffRate * 100
              },
              {
                key: "负荷率(%)",
                value: res.data.fuheRate * 100
              }
            ]
          } catch (e) {
            return false
          }
        } else {
          vm.echartInstence.setOption({
            title: {
              text: "暂无数据",
              textStyle: {
                fontSize: 28,
                fontWeight: "normal"
              },
              left: "center",
              top: "center"
            }
          })
          vm.echartInstence.hideLoading()
          vm.tableData = [
            {
              key: "最大负荷(kW)",
              value: "-"
            },
            {
              key: "发生时间",
              value: "-"
            },
            {
              key: "最小负荷(kW)",
              value: "-"
            },
            {
              key: "发生时间",
              value: "-"
            },
            {
              key: "平均负荷(kW)",
              value: "-"
            },
            {
              key: "峰谷差(kW)",
              value: "-"
            },
            {
              key: "峰谷差率(%)",
              value: "-"
            },
            {
              key: "负荷率(%)",
              value: "-"
            }
          ]
        }
      })
    },

    printChart(optionArr, name, date) {
      const vm = this
      let xRay = []
      let series1 = [], // 平均值
        series2 = [], // 最大值
        series3 = [] // 最小值
      optionArr.forEach(item => {
        series1.push(item.avg)
        series2.push(item.max)
        series3.push(item.min)
        xRay.push(item.id)
      })
      let option = {
        title: {
          text: name,
          left: "center",
          subtext: date
        },
        color: [...this.color],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985"
            }
          }
        },
        legend: {
          bottom: 0
        },
        grid: {
          top: "20%",
          left: "3%",
          right: "4%",
          bottom: "50"
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: xRay
        },
        yAxis: {
          name: "单位(kW)",
          type: "value"
        },
        series: [
          {
            type: "line",
            name: "平均-负荷",
            data: series1
          },
          {
            type: "line",
            name: "最大-负荷",
            data: series2
          },
          {
            type: "line",
            name: "最小-负荷",
            data: series3
          }
        ]
      }
      vm.echartInstence.setOption(option)
      vm.echartInstence.hideLoading()
    },

    backToLast() {
      this.$router.go(-1)
    }
  }
}
</script>
